﻿namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken token = cts.Token;

            Task.Run(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    // 一些异步操作
                    Console.WriteLine("异步执行中");
                    Thread.Sleep(500);
                }

            }, token);

            Thread.Sleep(2000);
            // 取消任务
            cts.Cancel();
            Console.ReadLine();
        }
    }
}